<?php

namespace App\Controller;

use App\Grpc\CalculateClient;
use Grpc\CalculateRequest;
use Hyperf\HttpServer\Contract\RequestInterface;

class CalculateController extends AbstractController
{
    /**
     * @var CalculateClient
     */
    public $client;

    public function __construct(CalculateClient $client)
    {
        $this->client = $client;
    }

    public function add(RequestInterface $request)
    {
        $calc = new CalculateRequest();
        $calc->setA($request->query("a",10));
        $calc->setB($request->query("b",5));
        $response = $this->client->add($calc);
        return [
            "code" => 0 , "msg"=>"ok" ,"data"=>["result"=>$response->getRes()]
        ];
    }

    public function sub(RequestInterface $request)
    {
        $calc = new CalculateRequest();
        $calc->setA($request->query("a",10));
        $calc->setB($request->query("b",5));
        $response = $this->client->sub($calc);
        return [
            "code" => 0 , "msg"=>"ok" ,"data"=>["result"=>$response->getRes()]
        ];
    }

    public function mul(RequestInterface $request)
    {
        $calc = new CalculateRequest();
        $calc->setA($request->query("a",10));
        $calc->setB($request->query("b",5));
        $response = $this->client->mul($calc);
        return [
            "code" => 0 , "msg"=>"ok" ,"data"=>["result"=>$response->getRes()]
        ];
    }

    public function div(RequestInterface $request)
    {
        $calc = new CalculateRequest();
        $calc->setA($request->query("a",10));
        $calc->setB($request->query("b",5));
        $response = $this->client->div($calc);
        return [
            "code" => 0 , "msg"=>"ok" ,"data"=>["result"=>$response->getRes()]
        ];
    }
}